package sy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.PageHelper;
import sy.pageModel.Route;
import sy.service.RouteServiceI;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * 产品控制器
 * 
 * 
 */
@Controller
@RequestMapping("/routeController")
public class RouteController extends BaseController {

	@Autowired
	private RouteServiceI routeService;

	

	/**
	 * 跳转到产品管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "/business/route";
	}

	/**
	 * 获取产品数据表格
	 *
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(Route route, PageHelper ph) {
		return routeService.dataGrid(route, ph);
	}
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		Route route = new Route();
		route.setId(UUID.randomUUID().toString());
		request.setAttribute("route", route);
		return "/business/routeAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Route route) {
		Json j = new Json();
		try {
			routeService.add(route);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(route);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, String id) {
		Route route = routeService.get(id);
		request.setAttribute("route", route);
		return "/business/routeEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Route route) {
		Json j = new Json();
		try {
			routeService.edit(route);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(route);
		} catch (Exception e) {
			// e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		routeService.delete(id);
		j.setMsg("删除成功！");
		j.setSuccess(true);
		return j;
	}

	@RequestMapping("/batchDelete")
	@ResponseBody
	public Json batchDelete(String ids) {
		Json j = new Json();
		if (ids != null && ids.length() > 0) {
			for (String id : ids.split(",")) {
				if (id != null) {
					this.delete(id);
				}
			}
		}
		j.setMsg("批量删除成功！");
		j.setSuccess(true);
		return j;
	}
	@RequestMapping("/importExcel")
	public String importExcel() {
		return "/business/excelImport";
	}
	
    //导入
	@RequestMapping("/routeImport")
	@ResponseBody
	synchronized public Json saveroutewithExcelImport(@RequestParam("file") MultipartFile file){  
		Json j = new Json();
		String message=	routeService.saveroutewithExcel(file);
		j.setSuccess(true);
		j.setMsg(message);
		return j;
   }
}
